var table;
var layer;
var upload;
var form;
layui.use(['layer','table','upload','form','laydate'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#createDate'
    });
    laydate.render({
        elem: '#expireDate'
    });

    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#createUser").val(create.user_id);

    //轮播图列表状态
    form.on("radio(status)", function () {
        if( this.value==1) { //上线
            $("input[name=display]").val(1);
        }else if(this.value==2) {//下线
            $("input[name=display]").val(2);
        }
    })


    //上传图片
    upload.render({
        elem : '#uploadImgBtn',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {
            if(res.code==0){
                //主图截取
                $("#url").val(res.data);
                $("#img_divs").append("<img src='"+res.obj+"' style='width: 250px;height: 120px' id='imgS'/>");
            }
        },
        drag : true,
        accept : 'file',
        multiple : true,
        data : {}
    });



    //上传视频
    upload.render({
        elem : '#uploadBtn2',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {
            $("#div_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='"+res.obj+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );
            //视频截取
            var arrayImgs=res.obj;
            var courseUrl=arrayImgs.substr(arrayImgs.lastIndexOf(':')+2);
            $("#courseUrl").val(courseUrl);
            var vid = document.getElementById("my-video");
            vid.onloadedmetadata = function() {
                var courseTime=Math.ceil(vid.duration)
                $("#courseTime").val(courseTime);
            };
            element.progress('progressBar',100 + '%');
            setTimeout("fun()","2000");  //2000毫秒后执行test()函数，只执行一次。

        },
        drag : true,
        accept : 'video',
        multiple : true,
        data : { }
        ,progress: function(value){
            $("#progressBar").attr("style","display:block");
            if(value<99){
                element.progress('progressBar',value + '%');
            }

        }
    });

});
function fun() {
    $("#progressBar").attr("style","display:none");
}
//刷新
function refresh1() {
    location.reload();
}

//上传课程视频，进行判断
function uploadBtn(){
    //判断是否已有视频，在进行上传
    var num=$("#my-video").length;
    if(num>=1){
        layer.msg("您已添加过轮播视频", {icon: 5});
    }else{
        $('#uploadBtn2').click();
    }
}

//预览图点击展开
function uploadImg() {
    $("#up").attr("style","display:block");
    //预览图点击计数
    var num=0;
    num++;
    $("#num").val(num);
    $(".uploadImg2").attr("style","display:block");
    $("h1").remove(".h");
}

//收起预览图
function upUploadImg() {
    $(".uploadImg2").attr("style","display:none");
    $("#up").attr("style","display:none");
    $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
}

//添加轮播图
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){

        //判断是否添加过视频和图片
        var video=$("#my-video").length;
        var url=$("#url").val();

        if(url==""||url==null||url==undefined){
            layer.msg("请先添加课程图片", {icon: 5});
        }else if(video<1){
            layer.msg("请先添加课程视频", {icon: 5});
        }else{
            var formData = data.field;
            $.ajax({
                url:"/SyOperationController/addCarousel",
                type:"post",
                dataType:"json",
                traditional: true,//ajax传递数组必须添加属性
                data:formData,
                success:function(data){
                    if(data.code==0){
                        layer.msg('添加成功');
                        refresh1()
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
        }
        return false;
    });
});



//上传课程视频，进行判断
function uploadBtnImg(){
    //判断是否已有视频，在进行上传
    var num=$("#imgS").length;
    if(num>=1){
        layer.msg("您已添加过轮播图片", {icon: 5});
    }else{
        $('#uploadImgBtn').click();
    }
}
